<?php
class Mprovider extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="providers";
    }
    
    public function listAll() {
        $this->db->order_by("prov_name");
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->result_array();
        else return false;
    }
    
    public function listAllLimit($amount, $start) {
        $this->db->order_by("prov_name");
        $this->db->limit($amount, $start);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->result_array();
        else return false;
    }
    
    public function getById($id) {
        $this->db->where("prov_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function search($name, $address, $phone, $email, $area, $amount, $start) {
        $n=$add=$p=$e=$ar="'1=1'";
        if($name!="") $n="prov_name like '%".$name."%'";
        if($address!="") $add="prov_address like '%".$address."%'";
        if($phone!="") $p="prov_phone like '%".$phone."%'";
        if($email!="") $e="prov_email like '%".$email."%'";
        if($area!="") $ar="prov_area like '%".$area."%'";
        $str="select * from providers where $n and $add and $p and $e and $ar limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($name, $address, $phone, $email, $area) {
        $n=$add=$p=$e=$ar="'1=1'";
        if($name!="") $n="prov_name like '%".$name."%'";
        if($address!="") $add="prov_address like '%".$address."%'";
        if($phone!="") $p="prov_phone like '%".$phone."%'";
        if($email!="") $e="prov_email like '%".$email."%'";
        if($area!="") $ar="prov_area like '%".$area."%'";
        $str="select count(*) as 'count' from providers where $n and $add and $p and $e and $ar";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("prov_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("prov_id", $id);
        $this->db->delete($this->_table);
    }
    
    public function checkName($name, $id="") {
        if($id!="")
            $this->db->where("prov_id != ", $id);
        $this->db->where("prov_name", $name);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
    
    public function checkEmail($email, $id="") {
        if($id!="")
            $this->db->where("prov_id != ", $id);
        $this->db->where("prov_email", $email);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
}
?>
